﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_DL_Project_ManageClassUser : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string ClassUserNum = string.Empty;
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    TOUserOfClass objClassUser = new TOUserOfClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
                voClass = objClass.Load(CID);
                ClassName = voClass.Name;
                string search = GetSearch(CID);
                BinderPG(search);
            }
        }
    } 

    protected override void InitPage()
    {
        //lbl_InfoTitle_ManagerNew.Text = GetLanageValueByKey("lab_ManagerNew_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerNew.Text = "新闻列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch(int cid)
    {
        string search = "SELECT TO_UserOfClass.UOC_ID, TO_UserOfClass.UserID,  TO_Learner.UserName, TO_Learner.TeacherId, TO_Learner.Name,  TO_Learner.UserGroup, TO_Learner.UserPIC, TO_Learner.Mobile,  TO_Learner.Email, TO_Learner.ZhiCheng, TO_Learner.ZhiWu,  TO_Learner.Education,TO_UserOfClass.IsCourseFDY FROM TO_Learner ,TO_UserOfClass where TO_Learner.Learner_Id = TO_UserOfClass.UserID  and TO_UserOfClass.ClassID=" + cid;
        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
            search += string.Format(" and ( TO_Learner.Name like '%{0}%' or  TO_Learner.UserName like '%{0}%')", txt_KeyWord.Text.Trim());
        //search += " order by DLSchool.ID Desc";
        return search;
    }


    private void BinderPG(string search)
    {
        DataTable dt = obj.Query(search);
        ClassUserNum = dt.Rows.Count.ToString();

        dt.Columns.Add("CourseFDY");
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["IsCourseFDY"].ToString().Trim() == "1")
                    dt.Rows[i]["CourseFDY"] = "是";
                else
                    dt.Rows[i]["CourseFDY"] = "否";
            }
        }

        rptNew.DataSource = dt;
        rptNew.DataBind();
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
        BinderPG(search);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddClassUser.aspx?CID=" + ViewState["CID"].ToString();
        Script.RedirectURL(url);
    }
  
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "ldel")
        {
            if (objClassUser.Del(int.Parse(e.CommandArgument.ToString())))
                {
                    string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
                    BinderPG(search);
                }
                else
                {
                    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
           
        }
        else if (e.CommandName.ToLower() == "lkcfdy")
        {
            VOTOUserOfClass vouserofclass = objClassUser.Load(int.Parse(e.CommandArgument.ToString()));
            //if (vouserofclass.IsCourseFDY == 0)
            vouserofclass.IsCourseFDY = 1;
            if (objClassUser.Update(vouserofclass))
            {
                string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
                BinderPG(search);
            }
            else
            {
                lbl_msg.Text = "操作失败!";
            }
        }
        else if (e.CommandName.ToLower() == "lkcfdycannel")
        {
            VOTOUserOfClass vouserofclass = objClassUser.Load(int.Parse(e.CommandArgument.ToString()));
            if (vouserofclass.IsCourseFDY == 1)
                vouserofclass.IsCourseFDY = 0;

            if (objClassUser.Update(vouserofclass))
            {
                string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
                BinderPG(search);
            }
            else
            {
                lbl_msg.Text = "操作失败!";
            }
        }
       
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {

                    if (objClassUser.Del(int.Parse(lab_NewID.Text)))
                        {
                            
                        }
                        else
                        {
                            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                        }
                    
                }
            }
            string search = GetSearch(int.Parse(ViewState["CID"].ToString()));
            BinderPG(search);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
 
}